package pl.edu.agh.broker.producer;

import java.io.FileInputStream;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import pl.edu.agh.broker.mns.client.MultiNameServiceLocator;
import pl.edu.agh.broker.mns.shared.MultiNameService;

public class Main {

	private static final String BROKER_ID = "broker.id";
	private static final String MNS_ADDRESS = "mns.address";
	private static final String MNS_PORT = "mns.port";
	private static final String INTERVAL = "producer.interval";

	private static final String LOGGER_CONFIG = "src/main/resources/log4j.properties";
	private static final Logger LOGGER = Logger.getLogger(Main.class);
	private static final String PROPERTY_FILE = "conf/producer.properties";

	public static void main(String[] args) {

		// initialize log4j
		PropertyConfigurator.configure(LOGGER_CONFIG);
		try {
			// load properties
			Properties properties = new Properties();
			properties.load(new FileInputStream(PROPERTY_FILE));

			// resolve main broker address
			MultiNameService mns = MultiNameServiceLocator.getMultiNameService(
					properties.getProperty(MNS_ADDRESS), Integer
							.parseInt(properties.getProperty(MNS_PORT)));

			String brokerURL = (String) mns.getAny(properties
					.getProperty(BROKER_ID));
			long sleepInterval = Long.parseLong(properties
					.getProperty(INTERVAL));

			StockProducer stockProducer = new StockProducer(brokerURL,
					sleepInterval, mns);
			stockProducer.start();
			LOGGER.info("Stock Producer started");
			stockProducer.join();

		} catch (Exception e) {
			LOGGER.error("", e);
		}
	}

}
